package com.zzzzzz.config;

import java.net.UnknownHostException;

import javax.inject.Inject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.DatastoreImpl;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

@Configuration
public class DataConfig {
	
	@Inject
	private Environment environment;
	
	@Bean
	public Mongo mongo() throws UnknownHostException, MongoException {
		ServerAddress addr = new ServerAddress(environment.getProperty("mongo.host"), environment.getProperty("mongo.port", Integer.class));
		MongoOptions options = new MongoOptions();
		return new Mongo(addr, options);
	}

	@Bean
	public Morphia morphia() {
		return new Morphia();
	}
	
	@Bean
	public Datastore datastore() throws UnknownHostException, MongoException{
		Datastore datastore = new DatastoreImpl(morphia(), mongo(), environment.getProperty("mongo.dbname"));
		return datastore;
	}
	
}